package fr.oz.mobile.common 
{
	import caurina.transitions.Tweener;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import fr.digitas.flowearth.conf.Conf;
	import fr.oz.mobile.module.IModule;
	import fr.oz.mobile.module.WakeUpModule;
	/**
	 * ...
	 * @author Fazermokeur
	 */
	public class ModuleManager extends Sprite
	{
		private var a_historyId:Array = [];
		
		private var content:IModule;
		private var prevContent:IModule;
		
		private var module:IModule;
		
		public function ModuleManager() 
		{
			
		}
		
		public function setModule(moduleId:String):Boolean
		{
			if (Conf.getDatas(moduleId).data.children().length() == 0)	return false;
			
			trace("affiche " + moduleId);
			a_historyId.push(moduleId);
			trace(a_historyId.length);
			switch(moduleId) {
				case "wakeUpProfile":
					var wakeUpProfile:WakeUpModule = new WakeUpModule();
					wakeUpProfile.addEventListener(Event.CLOSE, handleCloseModule);
					switchView(wakeUpProfile);
				break;
			}
			return true;
		}
		
		private function switchView(m:IModule):void
		{
			if (content) {
				if (prevContent != null) {
					if (contains(prevContent as DisplayObject))	removeChild(prevContent as DisplayObject);
					prevContent = null;
				}
				prevContent = content;
				content = m;
				Tweener.addTween(prevContent, {alpha:0, time:1, onComplete:contentSwitched } );
			}else {
				content = m;
			}
			addChildAt((content as DisplayObject), 0);
			(content as DisplayObject).alpha = 0;
			Tweener.addTween(content, {alpha:1, time:1} );
		}
		
		private function contentSwitched():void
		{
			removeChild(prevContent as DisplayObject);
			content.activeView();
		}
		
		private function handleCloseModule(e:Event):void 
		{
			
		}
		
		public function goBack():Boolean
		{
			if (content.goBack())	return true;
			if (a_historyId.length == 1)	return false;
			
			a_historyId.pop();
			
			return true;
		}
	}

}